package com.wontube.smr.bo.sites.youtube.feed;

import java.io.IOException;

import org.dom4j.DocumentException;

import com.wontube.client.extension.utils.StringUtils;
import com.wontube.client.extension.utils.net.URLUtils;
import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.client.modle.vo.PagenateSupport;
import com.wontube.smr.client.constants.MovieGenres;
import com.wontube.smr.client.model.vo.MovieItems;

/**
 * @author yuechao
 *
 */
public final class YouTubeMovieFeedsBO {
	private YouTubeMovieFeedsBO() {
	}
	
	private static final String mostPopularUrlBase = "https://gdata.youtube.com/feeds/api/charts/movies/most_popular?region=US&v=2&alt=rss&paid-content=false&time=today";
	private static final String trendingUrlBase = "https://gdata.youtube.com/feeds/api/charts/movies/trending?region=US&v=2&alt=rss&paid-content=false&time=today";
	private static final String mostRecentUrlBase = "https://gdata.youtube.com/feeds/api/charts/movies/most_recent?region=US&v=2&alt=rss&paid-content=false&time=today";
	private static final String featuredUrlBase = "https://gdata.youtube.com/feeds/api/charts/movies/featured?region=US&v=2&alt=rss&paid-content=false&time=today";
	
	private static final String movieFeedUrlFormat = "http://gdata.youtube.com/feeds/api/videos?category=Movies&paid-content=false&q={}&v=2&alt=rss&start-index={}&max-results={}";
	
	public static final MovieItems search(String query, PagenateInfo pageInfo) throws IOException, DocumentException {
		String q = URLUtils.encodeURIComponent(query);
		
		String url = StringUtils.format(movieFeedUrlFormat, q, pageInfo.getStart(), pageInfo.getPageSize());
		return YouTubeFeedsBO.searchWithUrl(url);
	}
	
	public static final MovieItems listTopRated(PagenateInfo pageInfo) throws IOException, DocumentException {
		String url = StringUtils.format(movieFeedUrlFormat, "", pageInfo.getStart(), pageInfo.getPageSize());
		return YouTubeFeedsBO.searchWithUrl(url);
	}
	
	public static final MovieItems listTopRated(String genreName, PagenateInfo pageInfo) throws IOException, DocumentException {
		if (StringUtils.isNullOrEmpty(genreName)) {
			return new MovieItems();
		}
		Integer movieGenre = MovieGenres.allGenresMap.get(genreName);
		if (movieGenre == null) {
			return new MovieItems();
		}
		
		
		String url = StringUtils.format(movieFeedUrlFormat, "", pageInfo.getStart(), pageInfo.getPageSize())		
			+ "&movie_genre=" + movieGenre;
		
		
		return YouTubeFeedsBO.searchWithUrl(url);
	}
	
	public static final MovieItems selectPopularMovie(PagenateInfo pageInfo) throws IOException, DocumentException {
		MovieItems items = listWithUrlBase(null, mostPopularUrlBase, pageInfo);
		return items;
	}
	
	public static final MovieItems selectPopularMovie(String genreName, PagenateInfo pageInfo) throws IOException, DocumentException {
		MovieItems items = listWithUrlBase(genreName, mostPopularUrlBase, pageInfo);
		return items;
	}
	
	public static final MovieItems selectTopMovie(PagenateInfo pageInfo) throws IOException, DocumentException {
		MovieItems items = listWithUrlBase(null, trendingUrlBase, pageInfo);
		return items;
	}
	
	public static final MovieItems selectRecentMovie(PagenateInfo pageInfo) throws IOException, DocumentException {
		MovieItems items = listWithUrlBase(null, mostRecentUrlBase, pageInfo);
		return items;
	}
	
	public static final MovieItems selectRecentMovie(String genreName, PagenateInfo pageInfo) throws IOException, DocumentException {
		MovieItems items = listWithUrlBase(genreName, mostRecentUrlBase, pageInfo);
		return items;
	}
	
	public static final MovieItems selectFeaturedMovie(PagenateInfo pageInfo) throws IOException, DocumentException {
		MovieItems items = listWithUrlBase(null, featuredUrlBase, pageInfo);
		return items;
	}

	private static final MovieItems listWithUrlBase(final String genreName, final String urlBase, final PagenateInfo pageInfo) throws IOException, DocumentException {
		if (pageInfo.getLimit() > PagenateSupport.maxPageSize) {
			pageInfo.setPageSize(PagenateSupport.maxPageSize);
		}
		
		MovieItems movieItems = new MovieItems(pageInfo);		
		
		String url = urlBase + "&start-index=" + pageInfo.getStart() 
				+ "&max-results=" + pageInfo.getPageSize();
		
		if (!StringUtils.isNullOrEmpty(genreName)) {
			Integer movieGenre = MovieGenres.allGenresMap.get(genreName);
			if (movieGenre == null) {
				return movieItems;
			}

			url += "&movie-genre=" + movieGenre;
		}

		return YouTubeFeedsBO.searchWithUrl(url);
	}
}
